package com.ruoyi.web.controller.wlwsys.jintao;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.wlwsys.UserVo;
import com.ruoyi.system.domain.wlwsys.WlwsysPosition;
import com.ruoyi.system.mapper.wlwsys.WlwsysPositionMapper;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/position")
public class PositionController extends BaseController {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private WlwsysPositionMapper wlwsysPositionMapper;

    @Autowired
    private SysUserServiceImpl sysUserServiceImpl;


    @PostMapping("/getDepartmentList")
    public List<WlwsysPosition> getDepartmentList(){
        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        int[] booleans = new int[30];
        for (String key : keys)
        {
            LoginUser user = redisCache.getCacheObject(key);
            WlwsysPosition wlwsysPosition1 = wlwsysPositionMapper.selectById(user.getUserId());
            if(ObjectUtils.isNotEmpty(wlwsysPosition1)){
                booleans[Math.toIntExact(wlwsysPosition1.getId())] = 1;
            }
        }
        List<WlwsysPosition> wlwsysPositionList2 = wlwsysPositionMapper.selectAll();
        int i = 1;
        for(WlwsysPosition w:wlwsysPositionList2){
            w.setIs_online(booleans[i++]);
        }
        return wlwsysPositionList2;
    }

    //分页查询
    @PostMapping("/getPositionListByPageParam")
    public TableDataInfo getPositionListByPageParam(@RequestParam("pageNum")String pageNum,@RequestParam("pageSize")String pageSize){
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<WlwsysPosition> list = wlwsysPositionMapper.selectAllOrderPosition();
        return getDataTable(list);
    }

    //删除位置信息方法
    @PostMapping("/deletePositionById")
    public void deletePositionById(@RequestParam("positionId")Integer positionId){
        wlwsysPositionMapper.deleteMessage(positionId);
    }

    //更新信息方法
    @PostMapping("/updatePositionById")
    public void updatePositionById(String name,Long id,Integer positionId){
       wlwsysPositionMapper.updateMessage(positionId,name,id);
    }

    //更新下拉列表方法
    @PostMapping("/getUserList")
    public List<UserVo> getUserList(){
        return  sysUserServiceImpl.selectUserIdAndName();
    }
}
